<?php


namespace app\admin\services\hwObs;

class ObsServices
{
    private $file;

    protected $rules = [
        'upload/consult/'
    ];

    protected $save_nmae = [
        'uploads/consult/'
    ];

    protected $obs_name = [
        'consult',
    ];

    public function upload_obs_file($file, $rule = 0, $suffix)
    {
        $this->file = $file;

        $save = $this->rules[$rule];//$this->getFilePath($this->rules[$rule]);

        if (!file_exists($save)) {
            mkdir($save, 777, true);
        }
        $name = time() . rand('100000', '999999') . '.' . $suffix;
        $info = $this->file->move($save, $name);
        $key = $this->obs_name[$rule] . '/' . $info->getFileName();
        $path = $save . $info->getFileName();
        $obs_service = new  HWYObs();
        try {
            $url = $obs_service->uploadObs($path, $key);
            unlink($path);
            return $url;
        } catch (\Exception $e) {
            return '';
        }
    }

    public function getFilePath($path)
    {
        return root_path() . $path;
    }


    public function delete_obs_file($key)
    {
        try {
            $obs_service = new  HWYObs();
            $res = $obs_service->deleteObs($key);
            if ($res) {
                return $key . '-删除成功';
            } else {
                return $key . '-删除失败';
            }
        } catch (\Exception $e) {
            return $key . '-删除失败-' . $e->getMessage();
        }
    }
}